package cn.agiledata.bank.linkbank.action;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.ForeDict;
import cn.agiledata.bank.common.util.Md5Encrypt;
import cn.agiledata.bank.common.util.WebUtil;
import cn.agiledata.bank.linkbank.exception.LinkBankException;
import cn.agiledata.bank.linkbank.logic.payment.UnsPayResponse;
import cn.agiledata.bank.linkbank.service.BankNetPayService;
import cn.agiledata.bank.linkbank.service.BankPayService;
import cn.agiledata.bank.linkbank.service.BankTelPayService;
import cn.agiledata.bank.linkbank.service.WebPayService;

import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;

/**
 * 
 * @author jianglei
 * 
 */
public class BankPayResponseAction extends IPBBaseAction {

    private static final String BANK_CODE_KEY = "code";

    private static final String B2B_KEY = "b2b";
    
    private static final String INTL_KEY = "Intl";

	private BankPayService getBankPayService() {
        return (BankPayService) IPBAppFactory.getObj(ForeDict.SERVICE_BANK_PAY);
    }

    private BankNetPayService getBankB2cNetPayService() {
        return (BankNetPayService) IPBAppFactory
                .getObj(ForeDict.SERVICE_BANK_B2C_NET_PAY);
    }

    private BankNetPayService getBankB2bNetPayService() {
        return (BankNetPayService) IPBAppFactory
                .getObj(ForeDict.SERVICE_BANK_B2B_NET_PAY);
    }

    private BankTelPayService getBankTelPayService() {
        return (BankTelPayService) IPBAppFactory
                .getObj(ForeDict.SERVICE_BANK_TEL_PAY);
    }

    private WebPayService getWebPayService() {
        return (WebPayService) IPBAppFactory.getObj(ForeDict.SERVICE_WEB_PAY);
    }

    private BankNetPayService getBankIntlNetPayService() {
		return (BankNetPayService) IPBAppFactory
				.getObj(ForeDict.SERVICE_BANK_INTL_NET_PAY);
	}

    /**
	 * 允许未登录调用
	 */
    public int hasAccessPermission(int accessLevel, HttpServletRequest request)
            throws IPBException {
        return 1;
    }

    private void bankPayResponse(HttpServletRequest request, String bankCode,
            boolean b2b, boolean intl) throws LinkBankException {
        BankPayService bankPayService = null;
        if (b2b) { // b2b
            bankPayService = getBankB2bNetPayService();
        } else if(intl) { // 外卡支付
        	bankPayService = getBankIntlNetPayService();
        } else  {
            bankPayService = getBankPayService();
        }
        Map messages = WebUtil.getRequestParameters(request);
        String webRootUrl = WebUtil.getRoot(request);
        UnsPayResponse unsPayResponse = bankPayService.getUnsPayResponse(
                bankCode, messages, webRootUrl);
        
        String transId = unsPayResponse.getUnsTransId();
        getWebPayService().affirmTransaction(transId, unsPayResponse);
        request.setAttribute(UnsPayResponseAction.UNSPAYRESPONSE_KEY,
                unsPayResponse);
    }

    public ActionForward bankNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        /*Calendar c = Calendar.getInstance();
        int ms = c.get(Calendar.MILLISECOND);
        System.out.println("========== Date of response from bank: "
                + DateUtil.date2String(c.getTime(),
                        DateUtil.DATE_FORMAT_DATE_TIME) + "." + ms);*/
        String bankCode = request.getParameter(BANK_CODE_KEY);
        if (bankCode == null) {
            bankCode = (String) request.getAttribute(BANK_CODE_KEY);
        }
        // B2B支付标志
        boolean b2b = (Boolean) request.getAttribute(B2B_KEY) == Boolean.TRUE ? true
                : false;
        // 外卡支付标志
        boolean bolIntl = (Boolean) request.getAttribute(INTL_KEY) == Boolean.TRUE ? true
				: false;
        
        BankNetPayService bankNetPayService = null;
        if (b2b) { // b2b
            bankNetPayService = getBankB2bNetPayService();
        } else if (bolIntl) {
        	bankNetPayService = getBankIntlNetPayService();
        } else {
            bankNetPayService = getBankB2cNetPayService();
        }
        if (!bankNetPayService.validNetBankCode(bankCode)) {
            throw LinkBankException.UNSUPPORTED_NET_BANK;
        }
        bankPayResponse(request, bankCode, b2b, bolIntl);
        String retCode=request.getParameter("retCode");
        retCode=retCode!=null?retCode:"1";
        String key="JDEyW8iGw";
        String sign=Md5Encrypt.md5(retCode+key).toUpperCase();
        request.setAttribute("content",retCode+"|"+sign);
        request.setAttribute("bankCode", bankCode);
        return mapping.findForward(FORWARD_SUCCESS);
    }

    public ActionForward cibNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CIB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward cmbNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CMB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward icbcNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_ICBC);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward icbcB2bNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_ICBC);
        request.setAttribute(B2B_KEY, Boolean.TRUE);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward ccbNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CCB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward cncbCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_CNCB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward cmbcNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CMBC);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    /**
     * 华夏银行网上支付响应
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward hxbNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_HXB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward spdbCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_SPDB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward commCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_COMM);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward bocshCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_BOCSH);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward bocgdCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_BOCGD);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward gdbCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_GDB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward sdbCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_SDB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward postgcCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_POSTGC);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward shrcbCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_SHRCB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward bobCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_BOB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward gzcbCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_GZCB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    /**
     * 温州银行反馈
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward bowzCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_BOWZ);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    /**
     * 光大银行B2C反馈
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward cebNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CEB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }
    
    /**
     * 光大银行B2B反馈
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward cebB2bNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CEB);
        request.setAttribute(B2B_KEY, Boolean.TRUE);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }
    
    /**
     * 光大银行银联反馈
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward cebCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_CEB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }
    
    /**
     * 直连农业银行反馈接口
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward abcNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_ABC);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }
    
    public ActionForward abcCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_ABC);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }

    public ActionForward bankTelPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        String bankCode = request.getParameter(BANK_CODE_KEY);
        if (bankCode == null) {
            bankCode = (String) request.getAttribute(BANK_CODE_KEY);
        }
        if (!getBankTelPayService().validTelBankCode(bankCode)) {
            throw LinkBankException.UNSUPPORTED_TEL_BANK;
        }
        bankPayResponse(request, bankCode, false, false);
        return mapping.findForward(FORWARD_SUCCESS);
    }
    
    /**
     * 招商银行银联反馈
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward cmbCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_CMB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }
    
    /**
     * 工商银行银联反馈
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward icbcCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_ICBC);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }
    
    /**
     * 建设银行银联反馈
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward ccbCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_CCB);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }
    
    /**
     * 民生银行银联反馈
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward cmbcCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_CMBC);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }
    
    /**
     * 银联银联反馈
     * 
     * @param mapping
     * @param actionForm
     * @param request
     * @param response
     * @return
     * @throws Exception
     */
    public ActionForward chinapayCpNetPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CP_CHINAPAY);
        return bankNetPayResponse(mapping, actionForm, request, response);
    }



    public ActionForward cmbTelPayResponse(ActionMapping mapping,
            ActionForm actionForm, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        request.setAttribute(BANK_CODE_KEY, Constant.BANK_CODE_CMB);
        return bankTelPayResponse(mapping, actionForm, request, response);
    }
}
